<?php

namespace app\index\controller;


use app\index\model\Token;
use app\index\model\User;
use think\cache\driver\Redis;
use think\Db;
use think\Request;

class Index
{
    public function index()
    {
        echo "!23";
        // return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改（2006-2018） - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }

    //登录测试
    public function login(Request $request)
    {

        //接受参数
        $name = $request->post("name");
        $pwd = $request->post("password");
        $model = new User();


        $data = $model->getUserByMobile($name);

        if (empty($data)) {

            return json([
                'data' => [],
                'code' => 2000,
                'msg' => '账号或者密码错误'
            ]);
        }

        //加密密码
        $sign = $data['sign'];

        $logPwd = md5($sign . $pwd);
        //判断是否一致
        if ($data['password'] != $logPwd){
            return json([
                'data' =>[],
                'code' =>2000,
                'msg'=>'账号或者密码错误'
            ]);
        }

        //加密token
        $tokenModle = new Token();
        list($token, $key) = $tokenModle->NewToken([
            'user_id' => $data['id'],
            'mobile' => $data['mobile'],
        ]);
        //解密测试
        //$token = "5e58ce1110dab746bc83d33e700f9bb0aa383d165d41c8abe156bfb3f216dae22ab661811af436a087a2bab7f1c9590bd776884c";
        //$result = $tokenModle::decryptToken($token);

        //如果有过期那么redis 做过期判断
        $redis = new Redis();
        $redis->set('token_'.$token,json_encode($data),8600);

        return json([
            'data' => [
                'token' => $token,
                'key' => $key,
            ],
            'code' => 2000,
            'msg' => '登录成功'
        ]);


    }
}
